package com.mew.lobby.handler.gold;

import com.mew.common.dc.redis.UserProfileRedisService;
import com.mew.common.entity.user.Profile;
import com.mew.common.entity.user.User;
import com.mew.common.exception.ResponseCode;
import com.mew.common.rest.IntMessage;
import com.mew.common.rest.bo.MewSession;
import com.mew.lobby.annotation.LobbyHandler;
import com.mew.lobby.handler.sms.SMSValidService;
import com.mew.lobby.message.request.BindTelephoneRequestMessage;
import com.mew.lobby.net.LobbyHandlerMapCmdType;
import com.mew.lobby.net.MewHandler;
import com.mew.lobby.provide.GoldFeignService;
import com.mew.lobby.provide.UserFeignService;
import com.zebra.remoting.protocol.ZebraCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author dylin
 * @create 2021/5/24
 */
@Service
@Slf4j
@LobbyHandler(LobbyHandlerMapCmdType.TranSMS)
public class TranSMSHandler implements MewHandler<BindTelephoneRequestMessage> {
    @Autowired
    private SMSValidService smsValidService;
    @Autowired
    private UserProfileRedisService userProfileRedisService;
    @Autowired
    private UserFeignService userFeignService;

    @Override
    public void handle(ZebraCommand<BindTelephoneRequestMessage> zebraCommand, MewSession session) {
        String telephone = zebraCommand.getRequest().getTelephone();
        String authCode = zebraCommand.getRequest().getAuthCode();
        long uid = session.uid;

        User user = userProfileRedisService.getRedisUserById(uid);
        if (!user.getTelNumber().equals(telephone)){
            zebraCommand.sendFailureCode(ResponseCode.UserTelError.code());
            return;
        }
        int code = smsValidService.authCode(user.getTelNumber(), authCode);
        if (code==200){
            Profile profile = new Profile();
            profile.setUserId(uid);
            code = userFeignService.updateProfile(profile);

            zebraCommand.sendFailureCode(code);
            return;
        }

        zebraCommand.sendFailureCode(code);
    }
}
